// vote-edit.js
const util = require('../../utils/util.js')
const appConfig = require('../../config.js')
const dateTime = util.formatTime(new Date()).split(' ')
const app = getApp()

Page({
  data: {
    title: '',
    desc: '',
    options: ['', ''],
    author: '',
    isCheckBox: false,
    useWxAuth: false,
    date: dateTime[0],
    time: dateTime[1]
  },
  // 分享配置，在当前页分享，设置为首页
  onShareAppMessage: function (res) {
    var url = 'pages/index/index'
    return {
      title: '投票',
      path: url
    }
  },
  // 验证表单信息
  checkVote: function (event) {
    var values = event.detail.value
    var data = this.data
    data.title = values.voteTitle
    data.desc = values.voteDesc
    data.date = values.voteDate
    data.time = values.voteTime
    data.isCheckBox = values.isVoteCheckbox

    if (!data.title) {
      wx.showToast({
        title: '请输入投票标题！',
        icon: 'none'
      })
      return false
    }

    var options = data.options.filter(item => {
      return item
    })
    if (options.length < 2) {
      wx.showToast({
        title: '请输入至少两个选项！',
        icon: 'none'
      })
      return false
    }

    wx.showLoading({
      mask: true
    })

    this.createVote(data)
  },
  // 创建投票
  createVote: function (data) {
    console.log(data)
    var options = data.options.filter(item => {
      return item
    })
    app.requestWithCredentials({
      url: appConfig.APP_URL_PREFIX + 'poll/',
      method: 'POST',
      data: {
        title: data.title,
        desc: data.desc,
        choices: options,
        isMult: data.isCheckBox,
        endTime: data.date + ' ' + data.time
      },
      success: function (res) {
        if (res.data.code === 0) {
          var voteId = res.data.data.id
          wx.hideLoading()
          wx.showToast({
            icon: 'success'
          })
          setTimeout(function () {
            wx.navigateTo({
              url: '../vote-info/vote-info?voteId=' + voteId
            })
          }, 2000)
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.message
          })
        }
      }
    })
  },
  // 编辑选项
  editOption: function (event) {
    var options = this.data.options
    var index = event.currentTarget.dataset.index
    var value = event.detail.value
    options.splice(index, 1, value)
    this.setData({
      options: options
    })
  },
  // 删除选项
  removeOption: function (event) {
    console.log(index)
    var options = this.data.options
    var index = event.currentTarget.dataset.index
    options.splice(index, 1)
    this.setData({
      options: options
    })
  },
  // 添加新选项
  addVoteOption: function () {
    var options = this.data.options
    options.push('')
    console.log('options', options)
    this.setData({
      options: options
    })
  },
  // 选择日期
  changeDate: function (event) {
    var date = event.detail.value
    console.log(date)
    this.setData({
      date: date
    })
  },
  // 选择时间
  changeTime: function (event) {
    var time = event.detail.value
    console.log(time)
    this.setData({
      time: time
    })
  },
  onLoad: function () {
    wx.hideLoading()
    if (!app.globalData.userInfo) {
      this.setData({
        useWxAuth: true
      })
    }
  }
})
